/*
 * Copyright (c) 2013-2018 上海汇数数据. All rights reserved.
 * @(#) CipherKt.java 2018-08-06 17:26
 */

package cn.jh.common.core.crypto;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;


/**
 * JCA (Cipher) utility object.
 *
 * @author Fuchun
 * @since 1.0
 */
public class CipherKt {

    private static final Logger logger = LoggerFactory.getLogger(CipherKt.class);

    /**
     * JCA default security provider: `SunJCE`
     */
    public static final String SUN_JCE = "SunJCE";
    /**
     * BouncyCastle security provider: `BC`
     */
    public static final String BOUNCY_CASTLE = "BC";
    /**
     * 默认的密钥大小。
     */
    public static final int DEFAULT_KEY_SIZE = 128;
    /**
     * 每字节的位数。
     */
    public static final int BITS_PER_BYTE = 8;
    /**
     * 用于在流操作期间在流之间传输数据的内部缓冲区的默认大小（以字节为单位）
     */
    public static final int DEFAULT_STREAMING_BUFFER_SIZE = 512;
    /**
     * `SecureRandom` 实例的默认算法名称。
     */
    public static final String DEFAULT_SECURE_RANDOM_ALGORITHM_NAME = "SHA1PRNG";

    static  {
        // Add bouncy castle provider (BC)
        Security.addProvider(new BouncyCastleProvider());
    }

    public static SecureRandom getDefaultSecurRandom() {
        try {
            return SecureRandom.getInstance(DEFAULT_SECURE_RANDOM_ALGORITHM_NAME);
        } catch (NoSuchAlgorithmException ex) {
            logger.debug("The SecureRandom $DEFAULT_SECURE_RANDOM_ALGORITHM_NAME algorithm is not " +
                    "available on the current platform. Using the platform's default SecureRandom algorithm.");
            return new SecureRandom();
        }
    }
}
